package com.beagle.njddapp.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.owner.OilOrderBean;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hxx
 * @date 2021-03-19 18:46
 * 加油车首页订单列表
 */
public class OilOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<OilOrderBean> oilOrderBeanList;
    private Context context;
    private RecyclerViewItemClickLisenter<OilOrderBean> oilHomeBeanRecyclerViewItemClickLisenter;

    public void setOilHomeBeanRecyclerViewItemClickLisenter(RecyclerViewItemClickLisenter<OilOrderBean> oilHomeBeanRecyclerViewItemClickLisenter) {
        this.oilHomeBeanRecyclerViewItemClickLisenter = oilHomeBeanRecyclerViewItemClickLisenter;
    }

    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            OilOrderBean oilOrderBean= (OilOrderBean) view.getTag();
            oilHomeBeanRecyclerViewItemClickLisenter.onItemClickLisenter(oilOrderBean);
        }
    };

    public OilOrderAdapter(List<OilOrderBean> oilOrderBeanList, Context context) {
        this.oilOrderBeanList = oilOrderBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_oil_order,null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyHolder myHolder= (MyHolder) holder;
        OilOrderBean oilOrderBean=oilOrderBeanList.get(position);
        myHolder.tvNumber.setText(oilOrderBean.getMeshNum());
        myHolder.tvTime.setText(oilOrderBean.getTakeTime());
        myHolder.tvPhone.setText(oilOrderBean.getTakerPhone());
        myHolder.tvPhone.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        myHolder.tvPhone.getPaint().setAntiAlias(true);//抗锯齿
        if (TextUtils.isEmpty(oilOrderBean.getMeshAddress())) {
            if (!TextUtils.isEmpty(oilOrderBean.getMeshLatitude())) {
                GeocodeSearch geocoderSearch = new GeocodeSearch(context);
                geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {

                    @Override
                    public void onGeocodeSearched(GeocodeResult result, int rCode) {


                    }

                    @Override
                    public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
                        try {
                            myHolder.tvLocation.setText(result.getRegeocodeAddress().getFormatAddress());
                        } catch (Exception e) {

                        }
                    }
                });
                com.amap.api.services.core.LatLonPoint lp = new com.amap.api.services.core.LatLonPoint(Double.parseDouble(oilOrderBean.getMeshLatitude()), Double.parseDouble(oilOrderBean.getMeshLongitude()));
                RegeocodeQuery query = new RegeocodeQuery(lp, 200, GeocodeSearch.AMAP);
                geocoderSearch.getFromLocationAsyn(query);

            }
        } else {
            myHolder.tvLocation.setText(oilOrderBean.getMeshAddress());
        }
        if (oilOrderBean.isPayed()){
            myHolder.tvStatus.setText("已完成");
            myHolder.tvStatus.setTextColor(Color.parseColor("#0049FF"));
            myHolder.tvStatus.setBackground(context.getResources().getDrawable(R.drawable.bg_oil_finish));
        }else if ("canceled".equals(oilOrderBean.getOrderState())){
            myHolder.tvStatus.setText("已取消");
            myHolder.tvStatus.setTextColor(Color.parseColor("#58617A"));
            myHolder.tvStatus.setBackground(context.getResources().getDrawable(R.drawable.bg_oil_cancle));
        }else {
            myHolder.tvStatus.setText("处理中");
            myHolder.tvStatus.setTextColor(Color.parseColor("#E56600"));
            myHolder.tvStatus.setBackground(context.getResources().getDrawable(R.drawable.bg_oil_waitting));
        }
        myHolder.itemView.setTag(oilOrderBean);
        myHolder.itemView.setOnClickListener(onClickListener);


    }

    @Override
    public int getItemCount() {
        return oilOrderBeanList.size();
    }
    public class MyHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.item_number_tv)
        TextView tvNumber;
        @BindView(R.id.item_address_tv)
        TextView tvLocation;
        @BindView(R.id.item_concat_tv)
        TextView tvPhone;
        @BindView(R.id.item_start_tv)
        TextView tvTime;
        @BindView(R.id.item_status_tv)
        TextView tvStatus;


        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
